using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
/// <summary>
/// ���Ʋ�������
/// </summary>
public class IconMoveNode : TutorialEventNode
{
    [JsonAttribute("rect", "位置与大小（相对于屏幕的比列）")]
    public Rect startPoint = new Rect();

    [JsonAttribute("path", "路径点")]
    public Vector3 pointEnd = new Vector3();

    [JsonAttribute("time", "移动总时间")]
    public float duration = 0f;

    [JsonAttribute("ImPth", "图片路径")]
    public string showIcon = "";

    [JsonAttribute("anim", "使用动画控制器")]
    public bool useAnimator = false;
    
    [JsonAttribute("animParm", "动画状态", "anim")]
    public string animatorParm = "";

    [JsonAttribute("rept", "是否重复")]
    public bool Repeat = false;

    [JsonAttribute("cinput", "是否根据手指状态展示动画")]
    public bool checkInput = false;

    protected Image icon;
    protected Vector2 startPos;
    protected RectTransform transRect;
    protected Animator animator;

    //void StartMove()
    //{
    //    //animator.SetBool(animatorParm, true);
    //    this.icon.transform.localPosition = startPos;
    //    var tween = this.icon.transform.DOLocalMove(pointEnd, duration);
    //    tween.onComplete += OnEnd;
    //}


    //void OnEnd()
    //{
    //    if (this.Repeat)
    //    {
    //        StartMove();
    //    }
    //    else
    //    {
    //        Cancel();
    //    }
    //}

    /// <summary>
    /// 获取手指信息
    /// </summary>
    public void CheckFingerInput()
    {
        if (Vector3.Distance(transRect.localPosition, pointEnd) <= 0.001)
        {
            transRect.localPosition = startPos;
        }
        transRect.localPosition = Vector3.MoveTowards(transRect.localPosition, pointEnd,  5);
        
        // 按下
        if (InputUtils.GetMouseButtonDown(0))
        {
            this.icon.gameObject.SetActive(false);
        }

        //松手
        if (InputUtils.GetMouseButtonUp(0))
        {
            transRect.localPosition = startPos;
            this.icon.gameObject.SetActive(true);
        }
        
    }

    protected override void Init()
    {
        _tutorialManager.lateUpdate += CheckFingerInput;
        icon = TutorialUIManager.Instance.LoadImage(showIcon, startPoint);
        transRect = icon.GetComponent<RectTransform>();
        startPos = transRect.localPosition;
        pointEnd = TutorialUIManager.Instance.GetPositionWithPercent(pointEnd);
        //StartMove();
    }

    protected override void Cancel()
    {
        ClearData();
    }

    protected void ClearData()
    {
        _tutorialManager.lateUpdate -= CheckFingerInput;
        GameObject.DestroyObject(icon);
    }
}
